
package com.yimixian.app.order;

import com.yimixian.app.R;
import com.yimixian.app.model.ValidatedOrder;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class OrderGoodsListView extends LinearLayout {

    @InjectView(R.id.goods_name_text)
    TextView mGoodsNameText;
    @InjectView(R.id.goods_quantity_text)
    TextView mGoodsQuantityText;
    @InjectView(R.id.goods_price_text)
    TextView mGoodsPriceText;
    @InjectView(R.id.tv_give)
    TextView mTvGive;
    @InjectView(R.id.top)
    TextView mTop;
    @InjectView(R.id.bottom)
    TextView mBottom;
    @InjectView(R.id.tv_empty)
    TextView mTvEmpty;

    // @InjectView(R.id.order_comment_button)
    // TextView mCommentButton;

    public OrderGoodsListView(Context context) {
        super(context);
        initOrderGoodsListView();
    }

    public OrderGoodsListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initOrderGoodsListView();
    }

    public OrderGoodsListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initOrderGoodsListView();
    }

    private void initOrderGoodsListView() {
        LayoutInflater.from(getContext()).inflate(R.layout.order_goods_list_view, this);
        ButterKnife.inject(this);
        setOrientation(VERTICAL);
        setBackgroundColor(Color.WHITE);
    }

    public void bindView(ValidatedOrder.Items goodsItems, int i, int total) {
        if (i == 0) {
            mTop.setVisibility(View.VISIBLE);
        } else {
            mTop.setVisibility(View.GONE);
        }
        if (i == (total - 1)) {
            mBottom.setVisibility(View.VISIBLE);
        } else {
            mBottom.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(goodsItems.getTxtIndent())) {
            mTvEmpty.setText(goodsItems.getTxtIndent());
        }
        StringBuilder goodsNameBuilder = new StringBuilder();
        StringBuilder goodsQuantityBuilder = new StringBuilder();
        StringBuilder goodsPriceBuilder = new StringBuilder();
        /*
         * if (i > 0) { goodsNameBuilder.append(Constants.SEPARATOR);
         * goodsQuantityBuilder.append(Constants.SEPARATOR);
         * goodsPriceBuilder.append(Constants.SEPARATOR); }
         */
        String goodsName = goodsItems.getName();
        goodsNameBuilder.append(goodsName.length() > 10 ? goodsName.substring(0, 9) + "..."
                : goodsName);
        goodsQuantityBuilder.append(goodsItems.getCount());
        goodsPriceBuilder.append(goodsItems.getPrice() == null ? "" :
                goodsItems.getPrice());
        if (TextUtils.isEmpty(goodsItems.getItemAttr())) {
            mTvGive.setVisibility(View.GONE);
        } else {
            mTvGive.setVisibility(View.VISIBLE);
            mTvGive.setText(goodsItems.getItemAttr());
        }

        mGoodsNameText.setText(goodsNameBuilder.toString());
        mGoodsQuantityText.setText(goodsQuantityBuilder.toString());
        mGoodsPriceText.setText(goodsPriceBuilder.toString());
    }

    // public void bindFree(List<ValidatedOrder.FreeItems> goodsItems) {
    // StringBuilder goodsNameBuilder = new StringBuilder();
    // StringBuilder goodsQuantityBuilder = new StringBuilder();
    // for (int i = 0; i < goodsItems.size(); ++i) {
    // if (i > 0) {
    // goodsNameBuilder.append(Constants.SEPARATOR);
    // goodsQuantityBuilder.append(Constants.SEPARATOR);
    // }
    // String goodsName = goodsItems.get(i).getName();
    // goodsNameBuilder.append(goodsName.length() > 16 ? goodsName.substring(0,
    // 15) + "..."
    // : goodsName);
    // goodsQuantityBuilder.append(goodsItems.get(i).getCount());
    //
    // }
    // mGoodsNameText.setText(goodsNameBuilder);
    // mGoodsQuantityText.setText(goodsQuantityBuilder);
    // }

}
